﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;

namespace HA.ModFlow.Controls.UserControls
{
    /// <summary>
    /// winResetValue.xaml 的交互逻辑
    /// </summary>
    public partial class winResetValue : Window
    {
        public winResetValue(DataType type)
        {
            InitializeComponent();
           
            this._type = type;

            if (this._type == DataType.DOUBLE)
            {
                this.txtResetValue.Text = "0.0";
            }
            else
                this.txtResetValue.Text = "0";

            this.txtResetValue.Focus();
            this.btnOk.Click+=new RoutedEventHandler(btnOk_Click);
            this.btnCancel.Click+=new RoutedEventHandler(btnCancel_Click);
            //this.txtResetValue.TextChanged+=new TextChangedEventHandler(txtResetValue_TextChanged);
            //this.txtResetValue.KeyDown += new KeyEventHandler(txtResetValue_KeyDown);
            this.txtResetValue.LostFocus+=new RoutedEventHandler(txtResetValue_LostFocus);
        }

        #region 属性

        DataType _type;

        public DataType Type
        {
            get { return _type; }
            //set { _type = value; }
        }

        #endregion

        #region 事件

        private void btnOk_Click(object sender,RoutedEventArgs e)
        {

            if (this.labError.Visibility == Visibility.Hidden&this.txtResetValue.Text!="")
            {
                this.DialogResult = true;  
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            
            this.Close();
        }

        //private void txtResetValue_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    //屏蔽中文输入和非法字符粘贴输入
        //    TextBox textBox = sender as TextBox;
        //    TextChange[] change = new TextChange[e.Changes.Count];
        //    e.Changes.CopyTo(change, 0);

        //    int offset = change[0].Offset;
        //    if (change[0].AddedLength > 0)
        //    {
        //        double num = 0;
        //        if (!Double.TryParse(textBox.Text, out num)&&(textBox.Text!="-"))
        //        {
        //            textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
        //            textBox.Select(offset, 0);
        //        }
        //    }
        //    //检验输入数字类型
        //    if (this._type == DataType.INT)
        //    {
        //        if (this.txtResetValue.Text.Contains("."))
        //        {
        //            this.labError.Visibility = Visibility.Visible;
        //        }
        //        else
        //            this.labError.Visibility = Visibility.Hidden;
        //    }

        //}

        //private void txtResetValue_KeyDown(object sender, KeyEventArgs e)
        //{
        //    TextBox txt = sender as TextBox;

        //    //屏蔽非法按键
        //    if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal || e.Key == Key.Subtract)
        //    {
        //        if (txt.Text.Contains(".") && e.Key == Key.Decimal)
        //        {
        //            e.Handled = true;
        //            return;
        //        }
        //        else if (txt.Text.Contains("-") && e.Key == Key.Subtract)
        //        {
        //            e.Handled = true;
        //            return;
        //        }

        //        e.Handled = false;
        //    }

        //    else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod || e.Key == Key.OemMinus) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
        //    {
        //        if (txt.Text.Contains(".") && e.Key == Key.OemPeriod)
        //        {
        //            e.Handled = true;
        //            return;
        //        }
        //        e.Handled = false;
        //    }

        //    else
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtResetValue_LostFocus(object sender, RoutedEventArgs e)
        {
           if(this._type==DataType.DOUBLE)
            {
                if (!this.txtResetValue.Text.Contains("."))
                {

                    this.txtResetValue.Text = this.txtResetValue.Text + ".0";
                }
            }
        }

        #endregion

    }
}
